package com.lh.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.project.common.CacheClient;
import com.lh.project.mapper.FoodMapper;
import com.lh.project.model.entity.Food;
import com.lh.project.model.enums.RedisConstants;
import com.lh.project.model.enums.SystemConstants;
import com.lh.project.service.FoodService;
import com.lh.project.service.MuzzleService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.lh.project.model.enums.RedisConstants.*;

/**
*
*/
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food>
implements FoodService {

    @Resource
    private CacheClient cacheClient;

    @Resource
    private MuzzleService muzzleService;

    @Resource
    private FoodMapper foodMapper;

    @Override
    public List<Food> queryFoodList(long id, int current) {
        System.out.println("请求参数id="+id+",current="+current);
        int count = foodMapper.getCountByFoodSortId(id);
        System.out.println("该类型的food总共有："+count);
        if(count <= SystemConstants.DEFAULT_PAGE_SIZE *(current-1)){
            return null;
        }
        Page<Food> page1 = new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE);
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("food_sort",id);
        Page<Food> page = page(page1, queryWrapper);
        System.out.println(page.getRecords());
        for(int i = 0;i < page.getRecords().size();i++){
            Food food = page.getRecords().get(i);
            page.getRecords().get(i).setFoodIdStr(food.getFoodId().toString());
        }
        return page.getRecords();
    }

    @Override
    public void addSellCount(Integer orderCount, Long orderFoodId) {
        //在给food加count的时候，也得给muzzle加哦~
        Food food = getById(orderFoodId);
        foodMapper.addSellCount(orderCount,orderFoodId);
        muzzleService.addSellCount(orderCount,food.getFoodMuzzleId());
    }

    /**
     * 结合redis实现获取food的功能
     * @param id
     * @return
     */
    @Override
    public Food myGetById(long id) {

        Food food = cacheClient.queryWithPassThrough(id, RedisConstants.Food_CACHE, Food.class, this::getById, 20L, TimeUnit.SECONDS);
        if(food==null){
            //发生了缓存击穿！
        }
        return food;
    }

    @Override
    public List<Food> queryFoodListByRedis(long id, int current) {
        //使用String数据结构
//        List<Food> foodList = cacheClient.queryWithLogicalExpireByString(id, current, Food_LIST_CACHE_STRING, this::list, Food.class, "food_sort",60,TimeUnit.SECONDS,LOCK_FOOD_KEY);
        //使用hash数据结构
        List<Food> foodList = cacheClient.queryWithLogicalExpireByHashMap(id, current, Food_LIST_CACHE_HASHMAP,this::list, Food.class, "food_sort",60,TimeUnit.SECONDS,LOCK_FOOD_KEY);
        return foodList;
    }
}
